<template>
  <!-- 产品管理 -->
  <div class="app">
    <div class="box">
      <div class="top" v-if="searchFlag == true">
        <el-form ref="form" :model="form" label-width="80px" :inline="true">
          <el-form-item label="产品名字:">
            <el-input
              clearable
              v-model="form.prodName"
              style="width: 220px"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态:">
            <el-select
              clearable
              v-model="form.status"
              placeholder="请选择活动区域"
            >
              <el-option label="上架" :value="1"></el-option>
              <el-option label="未上架" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList"
            >搜索</el-button
          >
          <el-button icon="el-icon-delete" @click="clearFun">清空</el-button>
        </el-form>
      </div>
      <div class="btn">
        <div class="left">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="goAdd"
            >新增</el-button
          >
          <el-button
            @click="delList2()"
            type="danger"
            size="small"
            :disabled="disabled"
            >批量删除</el-button
          >
        </div>
        <div class="right">
          <el-button icon="el-icon-refresh" circle></el-button>
          <el-button
            icon="el-icon-search"
            circle
            @click="searchBtn"
          ></el-button>
        </div>
      </div>
      <div class="tag">
        <el-tag size="medium"
          >当前已选择<span>{{ selectionNum }}</span
          >项 <span @click="selectClear">清空</span></el-tag
        >
      </div>
      <div class="content">
        <el-table
          border
          :data="tableData"
          ref="multipleTable"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          :header-cell-style="{ backgroundColor: '#FAFAFA', color: '#000' }"
        >
          <el-table-column type="selection" width="50" align="center">
          </el-table-column>
          <el-table-column
            prop="prodName"
            label="产品名字"
            width="128"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="oriPrice"
            label="产品原价"
            width="127"
            align="center"
          >
          </el-table-column>
          <el-table-column
            width="127"
            prop="price"
            label="产品现价"
            align="center"
          >
          </el-table-column>
          <el-table-column
            width="127"
            prop="totalStocks"
            label="商品库存"
            align="center"
          >
          </el-table-column>
          <el-table-column
            width="150"
            prop="imgs"
            label="产品图片"
            align="center"
          >
            <template slot-scope="scope">
              <img :src="scope.row.imgs" alt="" />
            </template>
          </el-table-column>
          <el-table-column
            width="127"
            prop="status"
            label="状态"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag>{{ scope.row.status == 1 ? "上架" : "未上架" }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column width="350" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit">修改</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="delList(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="abc"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getList, delFun } from "@/api/wlgd";
export default {
  data() {
    return {
      form: {
        prodName: "",
        status: "",
      },
      searchFlag: true, //控制搜索输入框显示隐藏
      disabled: true, //控制批量删除是否禁用
      selectionNum: 0,
      size: 10,
      abc: null,
      currentPage: 1,
      tableData: [],
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //数据接口
    getList() {
      getList({
        current: this.currentPage,
        size: this.size,
        status: this.form.status,
        prodName: this.form.prodName,
      }).then((res) => {
        console.log(res);
        this.tableData = res.data.records;
        this.abc = res.data.total;
        console.log(this.tableData);
      });
    },
    //去新增
    goAdd() {
      this.$router.push({
        path: "managecom",
      });
    },
    //删除点击事件
    delList(row) {
      this.$confirm("确定进行[删除]操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delFun(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //删除接口
    delFun(row) {
      delFun([row.prodId]).then((res) => {
        console.log(res);
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        this.getList();
      });
    },
    //批量删除点击事件
    delList2() {
      this.$confirm("确定进行[批量删除]操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delFun2();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 批量删除
    delFun2() {
      delFun(this.multipleSelection).then((res) => {
        console.log(res);
        this.getList();
      });
    },
    //清空输入框
    clearFun() {
      this.form.prodName = "";
      this.form.status = "";
    },
    //清空复选框
    selectClear() {
      this.$refs.multipleTable.clearSelection();
    },
    //复选框
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i = 0; i < val.length; i++) {
        this.multipleSelection.push(val[i].prodId);
      }
      console.log(this.multipleSelection);
      this.selectionNum = val.length;
      if (this.multipleSelection.length > 0) {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
    },
    //控制搜索显示隐藏
    searchBtn() {
      this.searchFlag = !this.searchFlag;
    },
    //分页器
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getList();
    },
  },
};
</script>

<style scoped lang="scss">
.app {
  padding: 20px;
  background-color: #f1f4f5;
}
.box {
  padding: 20px;
  background-color: #fff;
  .btn {
    display: flex;
    justify-content: space-between;
  }
}
.tag {
  margin: 20px 0;
}
.el-tag {
  background-color: rgba(2, 161, 233, 0.1);
  padding: 0 10px;
  height: 32px;
  line-height: 30px;
  font-size: 12px;
  color: #02a1e9;
  border-radius: 4px;
  box-sizing: border-box;
  border: 1px solid rgba(2, 161, 233, 0.2);
  white-space: nowrap;
  span:nth-of-type(1) {
    font-size: 16px;
    font-weight: 600;
    padding: 0px 4px;
  }
  span:nth-of-type(2) {
    margin-left: 4px;
    font-weight: 600;
    cursor: pointer;
  }
}
</style>